package com.yxdj.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yxdj.common.core.web.domain.AjaxResult;
import com.yxdj.common.security.utils.SecurityUtils;
import com.yxdj.order.api.domain.OrderInfo;
import com.yxdj.order.domain.dto.ServiceFeeDto;
import com.yxdj.order.service.OrderInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author 韩总
 */
@Tag(name = "工程师订单管理")
@RestController
@RequestMapping("/appEngineer")
public class AppEngineerOrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @Operation(summary = "工程师查询待抢订单")
    @GetMapping("/getOrderForGrab")
    public AjaxResult receiveOrder(){
        //获取待抢订单
        List<OrderInfo> orderInfoList = orderInfoService.list(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getOrderStatus, 0));
        return AjaxResult.success(orderInfoList);
    }

    @Operation(summary = "工程师接单")
    @PostMapping("/receiveOrder/{orderId}")
    public AjaxResult receiveOrder(@PathVariable("orderId") Long orderId){
        //接单
        orderInfoService.update(new LambdaUpdateWrapper<OrderInfo>().set(OrderInfo::getOrderStatus, 1).set(OrderInfo::getEngineerId, SecurityUtils.getUserId()).set(OrderInfo::getUpdateTime,new Date()).eq(OrderInfo::getId, orderId));
        return AjaxResult.success();
    }

    @Operation(summary = "工程师查询自己不同状态的订单")
    @GetMapping("/getEngineerOrder/{status}")
    public AjaxResult getEngineerOrder(@PathVariable("status") Integer status){
        //获取工程师id
        Long engineerId = SecurityUtils.getUserId();
        //获取工程师订单
        List<OrderInfo> orderInfoList = orderInfoService.list(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getEngineerId, engineerId).eq(OrderInfo::getOrderStatus, status));
        return AjaxResult.success(orderInfoList);
    }

    @Operation(summary = "工程师输入上门费用和维修费用")
    @PostMapping("/inputServiceFee")
    public AjaxResult inputServiceFee(@RequestBody ServiceFeeDto serviceFeeDto){
        LambdaUpdateWrapper<OrderInfo> orderInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        orderInfoLambdaUpdateWrapper.set(OrderInfo::getServiceFee, serviceFeeDto.getServiceFee())
                .set(OrderInfo::getRepairFee, serviceFeeDto.getRepairFee())
                .set(OrderInfo::getUpdateTime, new Date())
                .set(OrderInfo::getOrderStatus, 2)
                .eq(OrderInfo::getId, serviceFeeDto.getOrderId());
        //更新上门费用和维修费用
        orderInfoService.update(orderInfoLambdaUpdateWrapper);
        return AjaxResult.success();
    }
}
